/******************************************************************
 * Copyright (c) 2007 - 2007, Simon Guerard, All rights reserved.
 * 
 * Projet de jeu de course au large.
 * 
 * This File is part of Sail-Race
 * 
 * Sail-Race is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * Sail-Race is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Sail-Race; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.simcafamily.sailrace.vues;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import net.simcafamily.sailrace.core.AbstractView;
import net.simcafamily.sailrace.core.BaseVueJFrame;
import net.simcafamily.sailrace.core.IConstants;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xnap.commons.i18n.I18n;
import org.xnap.commons.i18n.I18nFactory;

import com.jgoodies.animation.Animation;
import com.jgoodies.animation.Animations;
import com.jgoodies.animation.Animator;
import com.jgoodies.animation.animations.BasicTextAnimation;
import com.jgoodies.animation.animations.BasicTextAnimations;
import com.jgoodies.animation.components.BasicTextLabel;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

@org.springframework.stereotype.Component("vueIntro")
public class VueIntro extends AbstractView {

    private class SelectUserActionListener implements ActionListener,
	    IConstants {
	@Override
	public void actionPerformed(ActionEvent event) {
	    JButton source = (JButton) event.getSource();
	    getSession().setAttribute(IConstants.ATTR_USER_SELECT,
		    source.getName());
	    baseVue.setActionName("infosActionSelectUser");
	    baseVue.getSynchro().set();
	}
    }

    private static final Log LOGGER = LogFactory.getLog(VueIntro.class);
    JPanel contentPane;
    I18n i18n = I18nFactory.getI18n(getClass());

    private Animator animator;

    // Construire le cadre
    /**
     * @wbp.parser.entryPoint
     */
    @Override
    public Component buildContentPane() {
	try {
	    contentPane = new JPanel();
	    contentPane.setOpaque(false);

	    final Font font = new Font("Tahoma", Font.BOLD, 22);
	    contentPane.setLayout(new FormLayout(new ColumnSpec[] {
		    ColumnSpec.decode("max(400px;pref):grow"),
		    ColumnSpec.decode("right:200px:grow"), },
		    new RowSpec[] { RowSpec.decode("531px"), }));

	    final BasicTextLabel label1 = new BasicTextLabel("TEST");
	    label1.setFont(font);
	    label1.setBounds(0, 0, 350, 100);
	    label1.setOpaque(false);
	    contentPane.add(label1, "1, 1, fill, fill");

	    final Animation welcome = BasicTextAnimation.defaultFade(label1,
		    2500, i18n.tr("Welcome"), Color.darkGray);
	    final Animation theJGoodiesAnimation = BasicTextAnimation
		    .defaultFade(label1, 3000, i18n.tr("Sail-Race"),
			    Color.darkGray);
	    final Animation description = BasicTextAnimations.defaultFade(
		    label1, label1, 2000, -100,
		    i18n.tr("A sail race offshore."), Color.darkGray);
	    final Animation features = BasicTextAnimation.defaultFade(label1,
		    3000, "", Color.darkGray);
	    final Animation featureList = BasicTextAnimations.defaultFade(
		    label1, label1, 1750, 0, "", Color.darkGray);
	    final Animation all = Animations.sequential(new Animation[] {
		    Animations.pause(1000), welcome, Animations.pause(1000),
		    theJGoodiesAnimation, Animations.pause(1000), description,
		    Animations.pause(1000), features, Animations.pause(1000),
		    featureList, Animations.pause(1500), });
	    animator = new Animator(all, 30);
	    animator.start();

	    JLayeredPane layeredPane = new JLayeredPane();
	    contentPane.add(layeredPane, "2, 1, left, fill");

	    layeredPane.setLayout(new FormLayout(new ColumnSpec[] {
		    FormFactory.UNRELATED_GAP_COLSPEC,
		    ColumnSpec.decode("89px"), ColumnSpec.decode("90px"), },
		    new RowSpec[] { RowSpec.decode("300px"),
			    RowSpec.decode("23px"),
			    FormFactory.UNRELATED_GAP_ROWSPEC,
			    RowSpec.decode("23px"),
			    FormFactory.UNRELATED_GAP_ROWSPEC,
			    RowSpec.decode("23px"),
			    FormFactory.UNRELATED_GAP_ROWSPEC,
			    RowSpec.decode("23px"), RowSpec.decode("67px"),
			    FormFactory.RELATED_GAP_ROWSPEC,
			    RowSpec.decode("23px"),
			    FormFactory.RELATED_GAP_ROWSPEC,
			    FormFactory.DEFAULT_ROWSPEC,
			    RowSpec.decode("28px"), }));

	    JButton btnGamer_1 = new JButton(i18n.tr("Gamer 1"));
	    btnGamer_1.setAlignmentX(Component.CENTER_ALIGNMENT);
	    btnGamer_1.addActionListener(new SelectUserActionListener());
	    btnGamer_1.setName("user_1");
	    layeredPane.add(btnGamer_1, "2, 2, fill, fill");

	    JButton btnGamer_2 = new JButton(i18n.tr("Gamer 2"));
	    btnGamer_2.addActionListener(new SelectUserActionListener());
	    btnGamer_2.setName("user_2");
	    btnGamer_2.setAlignmentX(Component.CENTER_ALIGNMENT);
	    layeredPane.add(btnGamer_2, "2, 4, fill, fill");

	    JButton btnGamer_3 = new JButton(i18n.tr("Gamer 3"));
	    btnGamer_3.addActionListener(new SelectUserActionListener());
	    btnGamer_3.setName("user_3");
	    btnGamer_3.setAlignmentX(Component.CENTER_ALIGNMENT);
	    layeredPane.add(btnGamer_3, "2, 6, fill, fill");

	    JButton btnGamer_4 = new JButton(i18n.tr("Gamer 4"));
	    btnGamer_4.addActionListener(new SelectUserActionListener());
	    btnGamer_4.setName("user_4");
	    btnGamer_4.setAlignmentX(Component.CENTER_ALIGNMENT);
	    layeredPane.add(btnGamer_4, "2, 8, fill, fill");

	    JButton btnQuitter = new JButton(i18n.tr("Quit"));
	    btnQuitter.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
		    BaseVueJFrame.quit(baseVue);
		}
	    });
	    btnQuitter.setAlignmentX(Component.CENTER_ALIGNMENT);
	    layeredPane.add(btnQuitter, "3, 11, fill, fill");

	} catch (final Exception e) {
	    VueIntro.LOGGER.error(e.getMessage(), e);
	}
	return contentPane;
    }

    /**
     * @return the animator
     */
    public final Animator getAnimator() {
	return animator;
    }
}
